package com.peng.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.peng.base.exception.GuliException;
import com.peng.user.entity.UcenterMember;
import com.peng.user.entity.vo.LoginVo;
import com.peng.user.entity.vo.RegisterVo;
import com.peng.user.mapper.UcenterMemberMapper;
import com.peng.user.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peng.utils.JwtUtils;
import com.peng.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Dengp
 * @since 2022-06-19
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    //登录方法
    @Override
    public String login(LoginVo loginVo) {
        String phone = loginVo.getMobile();
        String password = loginVo.getPassword();
        if (phone==null|| password==null){
            throw new GuliException(20001,"手机号或密码有误");
        }
        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getMobile,phone);
        UcenterMember ucenterMember = this.getOne(queryWrapper);
        if (ucenterMember==null){
            throw new GuliException(20001,"手机号不正确");
        }
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"密码错误");
        }
        if (ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"账户已被禁用");
        }
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }

    //注册账号
    @Override
    public void register(RegisterVo registerVo) {
        String phone = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        if (phone==null|| password==null){
            throw new GuliException(20001,"手机号或密码有误");
        }
        String code1 = redisTemplate.opsForValue().get(phone);
        if (!code1.equals(code)){
            throw new GuliException(20001,"验证码有误");
        }
        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getMobile,phone);
        int count = this.count(queryWrapper);
        if (count>0){
            throw new GuliException(20001,"用户已存在");
        }
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(registerVo.getNickname());
        ucenterMember.setMobile(registerVo.getMobile());
        ucenterMember.setPassword(MD5.encrypt(registerVo.getPassword()));
        ucenterMember.setAvatar("https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/65423f14-49a9-4092-baf5-6d0ef9686a85.png");
        this.save(ucenterMember);
        redisTemplate.delete(phone);
    }

    //根据openid获取用户
    @Override
    public UcenterMember getUserByOpenId(String openid) {
        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getOpenid,openid);
        UcenterMember one = this.getOne(queryWrapper);
        return one;
    }

    //根据日期统计当天注册人数
    @Override
    public int getRegisterCount(String day) {
        int count= baseMapper.getRegisterCount(day);
        return count;
    }
}
